import type { ResponseData, ResponsePaginationData } from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import { request } from 'umi';
import type {
  AddBillItem,
  ChargeSchemeDetail,
  ChargeSchemeTableItem,
  QueryListParams,
  AddBillRuleItem,
  updateBillRuleItem,
} from './data';

///////////////

type Pagination = { page?: number; pageSize?: number; size?: number };
/**
 * 获取规则列表
 * @param params
 */
export async function pageQuerySchemeList(
  params: Pagination & QueryListParams,
): Promise<RequestData<ChargeSchemeTableItem>> {
  const { page = 1, size = 10, name, type } = params;
  const res: ResponsePaginationData<{
    id: any;
    name: string;
    comment: string;
    type: number;
    update_time: string;
  }> = await request('/charge/scheme/page', {
    params: { page, size, schemeName: name, type },
  });
  if (res.code !== SUCCESS_CODE) return { success: false, total: 0, data: [] };

  return {
    success: true,
    total: res.data.total,
    data: res.data.list.map((item) => ({
      ...item,
      id: item.id,
      name: item.name,
      comment: item.comment,
      update_time: item.update_time,
    })),
  };
}
/**
 * 创建收费方案
 * @param params
 */
export async function addScheme(params: AddBillRuleItem) {
  const res: ResponseData<AddBillItem> = await request('/charge/scheme/add', {
    method: 'POST',
    data: params,
  });

  if (res.code === SUCCESS_CODE) {
    return { success: true, message: res.message, data: res.data };
  }
  return { success: false, message: res.message, data: res.data };
}
/**
 * 修改规则
 * @param params
 */
export async function updateScheme(params: updateBillRuleItem) {
  const res: ResponseData<ChargeSchemeDetail> = await request(`/charge/scheme/update`, {
    method: 'POST',
    data: params,
  });
  if (res.code === SUCCESS_CODE) {
    return { success: true, message: res.message, data: res.data };
  }
  return { success: false, message: res.message, data: res.data };
}

/**
 * 获取规则列表
 * @param params
 */
export async function getSchemeList(
  params: QueryListParams,
): Promise<RequestData<ChargeSchemeTableItem>> {
  const { name } = params;
  const res: ResponsePaginationData<{
    id: string;
    name: string;
    comment: string;
    pageSize: number;
  }> = await request('/charge/scheme/get_scheme_list', {
    params: { name },
  });
  if (res.code !== SUCCESS_CODE) return { success: false, total: 0, data: [] };

  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
    pageSize: 3,
  };
}

/**
 * 获取一个规则的详情 包括了 stages
 * @param id
 */
export async function getSchemeDetails(id: string): Promise<ChargeSchemeDetail | null> {
  const res: ResponseData<ChargeSchemeDetail> = await request(`/charge/scheme/${id}`);

  if (res.code === SUCCESS_CODE) {
    return res.data;
  }
  return null;
}

// 删除规则
export async function removeFeeRule(params: { id: string }): Promise<any> {
  const res: ResponseData<any> = await request(`/charge/scheme/${params.id}`, { method: 'DELETE' });
  if (res.code !== SUCCESS_CODE) {
    throw new Error(res.message);
  }
  return res;
}
